package BanyuanClub.Study.Day0309.锁相关3;

import java.util.concurrent.locks.ReentrantLock;

public class Guantou {
    public  int meat = 1;

    //可重入锁
    private ReentrantLock lock = new ReentrantLock();

    //同步锁的方法，锁的目标是this
    public  void eated(){
        lock.lock();
        try {
            if (this.meat > 0) {
                try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread() + "吃了1块肉");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.meat -= 1;
            }
            System.out.println("还剩下" + this.meat + "块肉");
        }finally {
            lock.unlock();
        }
    }
}
